/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * UI_Robots.java
 *
 * Created on May 3, 2010, 3:52:06 PM
 */

package UI;

import java.sql.SQLException;
import javax.swing.JTable;
import Modelo.BaseDato;
import com.mysql.jdbc.exceptions.jdbc4.CommunicationsException;
import robot_maker.Manager;
import robot_maker.ResultSetTableModel;
import Excepciones.BasesdeDatos;
import Excepciones.DinamicaDato;
import Excepciones.ProcesamientoDato;
import Excepciones.Sistema;
import robot_maker.Robot;
import java.sql.ResultSet;
import javax.swing.JOptionPane;




/**
 *
 * @author CarlosScipione
 */
public class UI_Robots extends javax.swing.JFrame {


    int rowMyor;
    /** Creates new form UI_Robots */
    public UI_Robots() {
        initComponents();
        this.setDefaultCloseOperation(HIDE_ON_CLOSE);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
     private void llenar (JTable tabla, String query) throws BasesdeDatos {
            try{
                BaseDato buscar = new BaseDato();
                tabla.setModel(new ResultSetTableModel(query));
            if (tabla.getRowCount() > 0) {
                tabla.setRowSelectionInterval(0, 0);
            }
        
        }catch (CommunicationsException ex) {
           Sistema.loggear(10, "LLENAR",
                        "COMUNICACION PERDIDA CON LA BASE DE DATOS",
                        UI_Robots.class, "CommunicationsException");
        }catch (SQLException ex) {
            Sistema.loggear(11, "LLENAR",
                        "EN EL SQL DE EJECUCCION",
                        UI_Robots.class, "SQLException");
        }catch (Exception ex) {
             Sistema.loggear(17, "LLENAR",
                        "EN EL SQL DE EJECUCCION",
                        UI_Robots.class, "GLOBAL");
        }

    }
    public void refrescar () throws BasesdeDatos{
         llenar(robot_table, "select id \"Codigo\", nombre \"Nombre\", " +
                 "descripcion \"Descripcion\", peso \"Peso\" from ROBOT");

    }
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        robot_table = new javax.swing.JTable();
        delete_boton = new javax.swing.JButton();

        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        jButton1.setText("+");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel1.setText("Robots/Modelos Disponbles:");

        robot_table.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        robot_table.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                robot_tableMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(robot_table);

        delete_boton.setText("-");
        delete_boton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                delete_botonActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(layout.createSequentialGroup()
                        .add(delete_boton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 38, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(jButton1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 37, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(org.jdesktop.layout.GroupLayout.LEADING, jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 367, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, jLabel1))
                .add(23, 23, 23))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                        .add(jLabel1)
                        .add(432, 432, 432))
                    .add(layout.createSequentialGroup()
                        .add(24, 24, 24)
                        .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 386, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(jButton1)
                            .add(delete_boton))))
                .addContainerGap(19, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        addRobot window = new addRobot(this, false, -1);
        window = (addRobot) Manager.centra(window);
        window.setVisible(true);
}//GEN-LAST:event_jButton1ActionPerformed

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
        try {
            llenar(robot_table, "select id \"Codigo\", nombre \"Nombre\", "
                    + "descripcion \"Descripcion\", peso \"Peso\" from ROBOT");
        } catch (BasesdeDatos ex) {
        }
      
    }//GEN-LAST:event_formWindowOpened

    private void robot_tableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_robot_tableMouseClicked
        rowMyor = robot_table.getSelectedRow() + 1;
        if(evt.getClickCount() == 2){
              ResultSet lista = null;
            try {
                lista = BaseDato.ejecutarSqlSelect("select id from ROBOT");
            } catch (DinamicaDato ex) {
            }
            try {
                int i =1;
                lista.next();
                while(i < rowMyor){
                    lista.next();
                    i++;
                }
            } catch (SQLException ex) {
        Sistema.loggear(11, "MouseClicked",
                        "EN EL SQL DE EJECUCCION",
                        UI_Robots.class, "SQLException");
            }

        addRobot window = null;
            try {
                window = new addRobot(this, true, lista.getInt(1));
            } catch (SQLException ex) {
        Sistema.loggear(11, "MouseClicked",
                        "EN EL SQL DE EJECUCCION",
                        UI_Robots.class, "SQLException");
            }
        window = (addRobot) Manager.centra(window);
        window.setVisible(true);
        }
    }//GEN-LAST:event_robot_tableMouseClicked

    private void delete_botonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_delete_botonActionPerformed
        Robot eliminar = new Robot();
        
        ResultSet lista = null;
        try {
            lista = BaseDato.ejecutarSqlSelect("select ID from ROBOT");
        } catch (DinamicaDato ex) {
        }
            try {
                int i =1;
                lista.next();
                while(i < rowMyor){
                    lista.next();
                    i++;
                }
                Object[] options = {"Si",
                    "No",
                    "Cancelar"};
        int n = JOptionPane.showOptionDialog(null,
            "Esto borrara toda la informacion del ROBOT incluyendo " +
            "compras de robots",
            "¿Esta seguro?",
            JOptionPane.YES_NO_CANCEL_OPTION,
            JOptionPane.QUESTION_MESSAGE,
            null,
            options,
            options[2]);
                eliminar.borrarBD(lista.getInt(1), n);
            } catch (ProcesamientoDato ex) {
            
            } catch (SQLException ex) {
                        Sistema.loggear(11, "DELETEBOTON",
                        "EN EL SQL DE EJECUCION",
                        UI_Robots.class, "SQLException");             }
        try {
            refrescar();
        } catch (BasesdeDatos ex) {
        }
    }//GEN-LAST:event_delete_botonActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new UI_Robots().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton delete_boton;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable robot_table;
    // End of variables declaration//GEN-END:variables

}
